package com.hksj.sglm.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.base.service.DefaultDeviceService;
import com.hksj.base.vo.DeviceVO;
import com.hksj.cbm.util.EasyExcelUtil;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import com.hksj.sglm.mapper.OxygenConcentrationMapper;
import com.hksj.sglm.query.OxygenConcentrationQuery;
import com.hksj.sglm.query.OxygenConcentrationTrendQuery;
import com.hksj.sglm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OxygenConcentrationService
 * @Description TODO
 * @Author @陈章
 * @Date 2024/9/12 17:30
 * @Version 1.0
 */
@Service
public class OxygenConcentrationService {
    @Autowired
    private OxygenConcentrationMapper oxygenConcentrationMapper;
    @Autowired
    private DefaultDeviceService defaultDeviceService;

    public Page<OxygenConcentrationVO> queryDeviceList(OxygenConcentrationQuery query) {
        //添加专业code
        query.setCode(SpecialityModelCodeEnum.special_gas_leakage.getCode());
        //创建分页对象
        Page<OxygenConcentrationVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        //设置禁止sql优化
        page.setOptimizeCountSql(false);
        List<OxygenConcentrationVO> list = oxygenConcentrationMapper.queryDeviceList(page, query);
        page.setRecords(list);
        return page;
    }

    public List<ConcentrationTrendVO> selectConcentrationTrendById(OxygenConcentrationTrendQuery query) {
        //根据设备id查询设备所在区域名称
        DeviceVO device = oxygenConcentrationMapper.deviceLocation(query.getId());
        //根据产品id、设备id查询设备的历史数据
        String productId = device.getProductId();
        String tableName = "properties_" + productId;
        List<ConcentrationTrendVO> list = oxygenConcentrationMapper.selectConcentrationTrendById(tableName, query.getId(), query.getSection());
        return list;
    }


    public Page<ConcentrationTrendVO> queryConcentrationTrendById(OxygenConcentrationTrendQuery query) {
        //创建分页对象
        Page<ConcentrationTrendVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        //设置禁止sql优化
        page.setOptimizeCountSql(false);
        //根据设备id查询设备所在区域名称
        DeviceVO device = oxygenConcentrationMapper.deviceLocation(query.getId());
        String productId = device.getProductId();
        String tableName = "properties_" + productId;
        int pageNum = (query.getPageNum() - 1) * query.getPageSize();
        List<ConcentrationTrendVO> list = oxygenConcentrationMapper.queryConcentrationTrendById(pageNum, query.getPageSize(), tableName, query.getId(), query.getSection());
        List<ConcentrationTrendVO> vos=oxygenConcentrationMapper.queryCountById(tableName,query.getId(), query.getSection());
        int count = vos.size();
        page.setRecords(list);
        page.setTotal(count);
        return page;
    }

    public void exportConcentrationTrend(OxygenConcentrationTrendQuery query, HttpServletResponse response) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        query.setDisAblePage(true);
        //根据设备id查询设备所在区域名称
        DeviceVO device = oxygenConcentrationMapper.deviceLocation(query.getId());
        String productId = device.getProductId();
        String tableName = "properties_" + productId;
        List<ConcentrationTrendVO> list = oxygenConcentrationMapper.selectConcentrationTrendById(tableName, query.getId(), query.getSection());
        List<ExportOxygenConcentrationTrendVO> exportList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            int i = 1;
            for (ConcentrationTrendVO vo : list) {
                ExportOxygenConcentrationTrendVO exportVO = new ExportOxygenConcentrationTrendVO();
                exportVO.setSeq(String.valueOf(i++));
                exportVO.setCreateTime(formatter.format(vo.getCreateTime()));
                exportVO.setValue(vo.getNumberValue());
                exportList.add(exportVO);
            }
        }
        String fileName = new StringBuffer()
                .append("特气泄露氧气浓度趋势")
                .append(new SimpleDateFormat("_yyyyMMddHHmmss").format(new Date()))
                .toString();

        EasyExcelUtil.exportExcelData(response, fileName, exportList, ExportOxygenConcentrationTrendVO.class);
    }
}
